<?php
namespace Admin\Controller;

/**
 * 后台管理-管理员控制器
 * 
 * @author Administrator
 *        
 */
class UserController extends CommController {
	
	public function _initialize() {
		parent::_initialize();
		// 模型
		$this->model = D( 'User' );
		// 参数
		$this->paramter['auth'] = I( 'post.auth', 0 );
		// 权限群组
		$this->getRoleList();
	}
	
	/**
	 * 前置方法：create
	 */
	public function _before_create() {
		if ( isset( $_POST['area'] ) && !empty( $_POST['area'] ) ) {
			$_POST['area'] = ",". implode( ",", $_POST['area'] ) .",";
		}
	}
	
	/**
	 * 前置方法：update
	 */
	public function _before_update() {
		if ( isset( $_POST['area'] ) && !empty( $_POST['area'] ) ) {
			$_POST['area'] = ",". implode( ",", $_POST['area'] ) .",";
		}
	}
	
	/**
	 * 管理员列表
	 */
	public function index() {
		// 获取参数
		$this->paramter['username'] = I( 'post.username', '' );
		$this->paramter['nickname'] = I( 'post.nickname', '' );
		// 设置查询条件
		$field = "a.id,a.username,a.nickname,a.status,a.create_time,a.last_login_time,a.last_login_ip";
		$field .= ",b.title as auth_title";
		
		if($_SESSION['userAuth']['id']==1)
		{
			$sql = "select $field from ".C('DB_PREFIX')."user as a";
			$sql .= " left join ".C('DB_PREFIX')."user_role as b on a.auth = b.id";
			$sql .= " where 1=1 and a.id <> 1 and a.is_delete = 0";
		}
		if($_SESSION['userAuth']['id']!=1)
		{
			$sql = "select $field from ".C('DB_PREFIX')."user as a";
			$sql .= " left join ".C('DB_PREFIX')."user_role as b on a.auth = b.id";
			$sql .= " where 1=1 and a.id <> 1 and a.is_delete = 0 and a.id=".$_SESSION['userAuth']['id'];
		}
		if ( !empty( $this->paramter['auth'] ) ) {
			// 群组
			$sql .= " and a.auth = {$this->paramter['auth']}";
		}
		if ( !empty( $this->paramter['username'] ) ) {
			// 用户名
			$sql .= " and a.username like '%{$this->paramter['username']}%'";
		}
		if ( !empty( $this->paramter['nickname'] ) ) {
			// 真实姓名
			$sql .= " and a.nickname like '%{$this->paramter['nickname']}%'";
		}
		
		// 读取数据
		$this->dataList = $this->getPageSQLList( $this->model, $sql );
		
		// 注入数据
		$this->assign( "paramter", $this->paramter );
		$this->assign( "dataList", $this->dataList );
		
		// 渲染视图
		$this->display ();
	}
	
	/**
	 * 获取权限群组列表
	 */
	private function getRoleList() {
		$roleList = D( 'UserRole' )->getList( array( 'is_delete' => 0, 'status' => 0 ), "id,title" );
		$this->assign( "roleList", $roleList );
	}
	
	/**
	 * 密码修改
	 */
	public function pass_edit() {
		$tag = I( 'get.tag', '' );
		if ( $tag == "update" ) {
			$return = $this->model->pass_edit ( $_POST );
			$this->dwzSuccess ( $return ['info'], $return ['status'], 'user', 'refreshClose' );
		} else {
			$theId = I( 'request.id', 0 );
			$return = $this->model->getCommOne ( $theId );
			if ($return ['status']) {
				$this->assign ( 'dataInfo', $return ['info'] );
			} else {
				$this->dwzSuccess( $return ['info'], 0, 'user', 'refreshClose' );
			}
			$this->display (); 
		}
	}
}